<?php
class Library_Model_LoaiVanBanMapper extends Library_Model_DataMapperAbstract
{
	private static $_instance = null;
	private $_paginator = null ;
	
	public function searchLoaiVanBanById($id)
	{
		$db = $this->getDb();
		$selectLoaivanban = $db->select()
		->from('tbl_loaivanban')
		->where('MaLoaiVanBan = ?',$id);
		$loaivanban = $db->fetchRow($selectLoaivanban);
	
		return $this->_populate($loaivanban);
	
	}
	public function loaivanbanList()
	{
		$db = $this->getDb();
		$selectLoaivanban = $db->select()
		->from('tbl_loaivanban');
		$loaivanbans = $db->fetchAll($selectLoaivanban);
	
		$loaivanbanObjArray = array();
		foreach($loaivanbans as $loaivanban){
			$loaivanbanObjArray[] = $this->_populate($loaivanban);
		}
	
		return $loaivanbanObjArray;
	}
	
	public function _populate($data){
		$obj = new Library_Model_LoaiVanBan();
		$obj->setId($data['MaLoaiVanBan']);
		$obj->setTenloai($data['TenLoai']);
		
		return $obj;		
	}
	
	protected function _insert(Library_Model_DomainObjectAbstract $obj){	
		$db = $this->getDb();
		$mavanban = $db->lastInsertId('tbl_vanban','MaVanBan');
		$data =array(
				'MaLoaiVanBan' => $obj->getId(),
				'TenLoai' => $obj->getTenLoai()				
				);
		$db->insert('tbl_loaivanban',$data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
		
		$db = $this->getDb();
		$data =array(
				'MaLoaiVanBan' => $obj->getId(),
				'TenLoai' => $obj->getTenLoai()
				);
		$where = $db->quoteInto('MaLoaiVanBan = ?', $obj->getId());
		$db->update('tbl_loaivanban',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaLoaiVanBan = ?',$id);
		$db->delete('tbl_loaivanban',$where);
	}
	public static function getInstance(){
		if(!isset(self::$_instance)){
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}
	
	}